{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.20 \u5904\u7406C\u8bed\u8a00\u4e2d\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5199C\u6269\u5c55\u4ee3\u7801\u5904\u7406\u6765\u81ea\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u6587\u4ef6\u6216\u751f\u6210\u5668\u4e2d\u7684\u5143\u7d20\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2aC\u6269\u5c55\u51fd\u6570\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u600e\u6837\u5904\u7406\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_consume_iterable(PyObject *self, PyObject *args) {\n  PyObject *obj;\n  PyObject *iter;\n  PyObject *item;\n\n  if (!PyArg_ParseTuple(args, \"O\", &obj)) {\n    return NULL;\n  }\n  if ((iter = PyObject_GetIter(obj)) == NULL) {\n    return NULL;\n  }\n  while ((item = PyIter_Next(iter)) != NULL) {\n    /* Use item */\n    ...\n    Py_DECREF(item);\n  }\n\n  Py_DECREF(iter);\n  return Py_BuildValue(\"\");\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e2d\u7684\u4ee3\u7801\u548cPython\u4e2d\u5bf9\u5e94\u4ee3\u7801\u7c7b\u4f3c\u3002\nPyObject_GetIter() \u7684\u8c03\u7528\u548c\u8c03\u7528 iter() \u4e00\u6837\u53ef\u83b7\u5f97\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\nPyIter_Next() \u51fd\u6570\u8c03\u7528 next \u65b9\u6cd5\u8fd4\u56de\u4e0b\u4e00\u4e2a\u5143\u7d20\u6216NULL(\u5982\u679c\u6ca1\u6709\u5143\u7d20\u4e86)\u3002\n\u8981\u6ce8\u610f\u6b63\u786e\u7684\u5185\u5b58\u7ba1\u7406\u2014\u2014 Py_DECREF() \u9700\u8981\u540c\u65f6\u5728\u4ea7\u751f\u7684\u5143\u7d20\u548c\u8fed\u4ee3\u5668\u5bf9\u8c61\u672c\u8eab\u4e0a\u540c\u65f6\u88ab\u8c03\u7528\uff0c\n\u4ee5\u907f\u514d\u51fa\u73b0\u5185\u5b58\u6cc4\u9732\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}